<template>
  <!-- 编辑框 -->
  <a-modal
      :title="title"
      :visible="dialog"
      @ok="submitForm"
      @cancel="handleCancel"
      width="500px"
  >
    <a-form-model ref="form"
                  :model="form"
                  :rules="rules">
      <a-row  >
        <a-col >
          <a-form-model-item label="二级学院名称" prop="name" class="row">
            <a-input v-model="form.name" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>

<script>
import {editCollege} from "@/services/college";
const defaultForm = {
  name: ''
};

export default {
  name: "FormDialog",
  props: {},
  data() {
    return {
      title: "添加",
      dialog: false,
      isModalVisible: false,
      loading: true,
      form: {},
      rules: {
        name: [
          { required: true, message: '请输入二级学院名称', trigger: 'blur' },
          { max: 32, message: "请输入二级学院名称不能超过32位", trigger: 'blur' }
        ],
      }
    };
  },
  computed: {},
  watch: {
  },
  created() {
    this.form = Object.assign({}, defaultForm);
  },
  methods: {
    resetForm() {
      this.form = Object.assign({}, defaultForm);
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          const name = this.form.name;
          const that = this;
          editCollege("", name)
              .then((resp) => {
                let result = resp.data;
                if (result.code == 200) {
                  that.$message.success("操作成功", 2);
                  this.$emit("success");
                  that.resetForm();
                }else {
                  that.$message.error(result.error)
                }
                that.dialog = false;
              })
              .catch((res) => {
                this.loading = false;
                this.$message.error(res.response.data.msg);
              });
          this.$emit("success");
          this.handleCancel();
        }
      });
    },
    handleCancel() {
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      this.form = Object.assign({}, defaultForm);
      this.loading = false;
      this.dialog = false;
    },
  },
};
</script>

<style type="text/css">
.row {
  margin: 0 auto;
}
</style>
